package com.jbm.jbmsupplier.util;

import java.io.File;
import java.util.ArrayList;

import com.jbm.jbmsupplier.model.JobInfo;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;

public class NetInterface extends Interface {
	public static final int NET_ERROR = 1001;
	
	public static final int CONFIRM_VER = 6000;
	public static final int LOGIN_RESULT = 6001;
	public static final int GET_JOB_LIST = 6002;
	public static final int UPLOAD_MEDIA = 6003;
	public static final int UPLOAD_MEDIA_BREAK = 6004;
	public static final int BREAK_JOB = 6005;
	public static final int COMFRIM_JOB = 6006;
	public static final int SUBMIT_JOB = 6007;
	
	
	private static String mServerIp = "rjcadmin.eicp.net";
	private static int mSockPort = 9999;
	private static int mTimeOut = 30000;
	private static boolean mIsConnect = false;
	private static NetInterface serverSocket = new NetInterface();
	private static Handler msgHandler = null;
	private static void Connect()
	{
        System.setProperty("java.net.preferIPv6Addresses", "false");
		String result = serverSocket.Connect(mServerIp, mSockPort, mTimeOut);
		if(result.equals("OK"))
		{
			mIsConnect = true;
		}
	}
	
	public static boolean IsConnect()
	{
		return mIsConnect;
	}
	
	private static Runnable LoginRunable = new Runnable(){
		@Override
		public void run()
		{
			
			try 
			{
				if(!IsConnect())
				{
					Connect();
				}
				if(IsConnect())
				{
					String result = serverSocket.Login(userName, userPwd);
					Message msg = new Message();
					msg.what = LOGIN_RESULT;
					msg.obj = result;
					msgHandler.sendMessage(msg);
					return;
				}
			} catch (Exception e)
			{  
			    e.printStackTrace();  
			} finally
			{  
			}  
		
			Message msg = new Message();
			msg.what = NET_ERROR;
			msgHandler.sendMessage(msg);
		}
	};
	private static Runnable GetJobListRunable = new Runnable(){
		@Override
		public void run()
		{
			
			try 
			{
				if(IsConnect())
				{
					JobList list = serverSocket.GetJobList(iStart, iEnd);
					for(int i = 0; i<list.count ; i++)
					{
						theJobs.add(new JobInfo(list.IndexofList(i)));
					}
					
					Message msg = new Message();
					msg.what = GET_JOB_LIST;
					msgHandler.sendMessage(msg);
					return;
				}
			} catch (Exception e)
			{  
			    e.printStackTrace();  
			}
			finally
			{  
			}
			
			Message msg = new Message();
			msg.what = NET_ERROR;
			msgHandler.sendMessage(msg);
			
		
		}
	};
	
	private static Runnable UploadMediaRunable = new Runnable(){
		@Override
		public void run()
		{
			
			boolean hasUpload = false;
			try 
			{
				if(IsConnect())
				{
					theJob.mediacount = theJob.fileList.size();
					for(int i=theJob.mediacount-1; i>=0; i--)
					{
						Media obj =JobInfo.ConvectToMedia(theJob.fileList.get(i));
						String srcFilePath = obj.filepath;
						File file = new File(srcFilePath);
						long filesize = file.length();
						boolean continueUpload = true;
						boolean fail = false;
						obj.offset = 0;
						while(continueUpload)
						{
							obj = NetInterface.serverSocket.UploadMedia(theJob.jobid, obj);
							if(obj.offset == -1)
							{
								fail= true;
								break;
							}
							if(obj.offset<filesize)
							{
								if(!IsConnect())
								{
									Connect();
									if(!IsConnect())
									{
										fail = true;
										break;
									}
								}
							}
						}
						if(fail)
							break;
						try
						{
							file.delete();
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
						theJob.fileList.remove(i);
						if(theDlg != null)
							theDlg.setProgress(i*100/theJob.mediacount);
						hasUpload = true;
					}
			         
					if(theDlg != null)
						theDlg.dismiss();
					
					Message msg = new Message();
					msg.what = UPLOAD_MEDIA;
					msgHandler.sendMessage(msg);
					return;
				}
			} catch (Exception e)
			{ 
			    e.printStackTrace();  
				if(hasUpload)
				{
					Message msg = new Message();
					msg.what = UPLOAD_MEDIA_BREAK;
					msgHandler.sendMessage(msg);
					return;
				}
			} 
			
			Message msg = new Message();
			msg.what = NET_ERROR;
			msgHandler.sendMessage(msg);
		
		}
	};

	private static Runnable StopOrderRunable = new Runnable(){
		@Override
		public void run()
		{
			
			try
			{
				if(IsConnect())
				{
					String result = serverSocket.BreakJob(theJob.jobid);;
					Message msg = new Message();
					msg.what = BREAK_JOB;
					msg.obj = result;
					msgHandler.sendMessage(msg);
					return;
				}
			} catch (Exception e)
			{  
			    e.printStackTrace();  
			} finally
			{  
			}  
		
			Message msg = new Message();
			msg.what = NET_ERROR;
			msgHandler.sendMessage(msg);
		}
	};
	private static Runnable FinishOrderRunable = new Runnable(){
		@Override
		public void run()
		{
			
			try
			{
				if(IsConnect())
				{
					String result = serverSocket.SubmitJob(theJob.jobid);;
					Message msg = new Message();
					msg.what = SUBMIT_JOB;
					msg.obj = result;
					msgHandler.sendMessage(msg);
					return;
				}
			} catch (Exception e)
			{  
			    e.printStackTrace();  
			} finally
			{  
			}  
		
			Message msg = new Message();
			msg.what = NET_ERROR;
			msgHandler.sendMessage(msg);
		}
	};
	private static Runnable ConfirmOrderRunable = new Runnable(){
		@Override
		public void run()
		{
			
			try
			{
				if(IsConnect())
				{
					String result = serverSocket.ConfirmJob(theJob.jobid);;
					Message msg = new Message();
					msg.what = COMFRIM_JOB;
					msg.obj = result;
					msgHandler.sendMessage(msg);
					return;
				}
			} catch (Exception e)
			{  
			    e.printStackTrace();  
			} finally
			{  
			}  
		
			Message msg = new Message();
			msg.what = NET_ERROR;
			msgHandler.sendMessage(msg);
		}
	};

	private static Runnable ConfirmVerRunable = new Runnable(){
		@Override
		public void run()
		{
			
			try
			{
				if(!IsConnect())
				{
					Connect();
				}
				if(IsConnect())
				{
					String result = serverSocket.ConfirmVer();;
					Message msg = new Message();
					msg.what = CONFIRM_VER;
					msg.obj = result;
					msgHandler.sendMessage(msg);
					return;
				}
			} catch (Exception e)
			{  
			    e.printStackTrace();  
			} finally
			{  
			}  
		
			Message msg = new Message();
			msg.what = NET_ERROR;
			msgHandler.sendMessage(msg);
		}
	};
	
	private static String userName = "";
	private static String userPwd = "";
	public static void Login(Handler mHandler, String strUName, String strUPwd) {
		msgHandler = mHandler;
		userName = strUName;
		userPwd = strUPwd;
		new Thread(LoginRunable).start();
	}

	private static ArrayList<JobInfo> theJobs =null;
	private static int iStart=0,iEnd=0;
	public static void RunGetJobList(Handler mHandler,ArrayList<JobInfo> jobs,int i, int j) {
		msgHandler = mHandler;
		theJobs = jobs;
		iStart = i;
		iEnd = j;
		new Thread(GetJobListRunable).start();
		
	}
	
	private static ProgressDialog theDlg = null;
	private static JobInfo theJob = null;
	public static void UploadMedia(Handler handler,ProgressDialog dlg,JobInfo job)
	{
		msgHandler = handler;
		theDlg = dlg;
		theJob = job;
		new Thread(UploadMediaRunable).start();
		
	}

	public static void StopOrder(JobInfo jobInfo, Handler handler) {
		msgHandler = handler;
		theJob = jobInfo;
		new Thread(StopOrderRunable).start();
		
	}

	public static void FinishOrder(JobInfo jobInfo, Handler handler) {
		msgHandler = handler;
		theJob = jobInfo;
		new Thread(FinishOrderRunable).start();
	}

	public static void ConfirmJob(JobInfo jobInfo, Handler handler) {
		msgHandler = handler;
		theJob = jobInfo;
		new Thread(ConfirmOrderRunable).start();
	}
	
	public static void ConfirmVer(Handler handler)
	{
		msgHandler = handler;
		new Thread(ConfirmVerRunable).start();
	}
	
}
